package com.software.project.controller;

import com.software.project.domain.Result;
import com.software.project.entity.UserInfo;
import com.software.project.exception.BizException;
import com.software.project.service.UserInfoService;
import com.software.project.utils.LogUtil;
import com.software.project.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/userInfo")
@Slf4j
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private HttpServletRequest request;

    // 新增用户信息
    @PostMapping("/createUserInfo")
    public Result<UserInfo> createUserInfo(@RequestBody UserInfo userInfo) {
        UserInfo create = userInfoService.createUserInfo(userInfo);
        log.info(LogUtil.success(request.getRemoteAddr(), create.toString()));
        return ResultUtil.success(create);
    }

    @GetMapping("/updateUserInfoStep")
    public Result<UserInfo> updateUserInfoStep(String userId, double step) {
        UserInfo userInfo = userInfoService.findUserInfoByUserId(userId);
        if (userInfo == null) {
            log.info(LogUtil.error(request.getRemoteAddr(), userId + " " + step));
            return ResultUtil.error("404", "error", null);
        } else {
            if (step > 0) {
                userInfo.setPower(userInfo.getPower() + (step - userInfo.getTodayStep()) * 0.001);
            }
            userInfo.setTodayStep((int)step);
            userInfo.setComprehensive(userInfo.getIntelligence()*0.5 + userInfo.getPower()*0.3 + userInfo.getCharm()*0.2);
            UserInfo create = userInfoService.createUserInfo(userInfo);
            log.info(LogUtil.success(request.getRemoteAddr(), userId + " " + step));
            return ResultUtil.success(create);
        }
    }

    //根据用户id查用户信息
    @GetMapping("/findUserInfoByUserId")
    public Result<UserInfo> findUserInfoByUserId(String userId) {
        UserInfo userInfo = userInfoService.findUserInfoByUserId(userId);
        if (userInfo == null) {
            log.info(LogUtil.error(request.getRemoteAddr(), userId));
            return ResultUtil.error("404", "error", null);
        } else {
            log.info(LogUtil.success(request.getRemoteAddr(), userId));
            return ResultUtil.success(userInfo);
        }
    }

    //综合榜中的省份榜
    @GetMapping("/findUserInfoByComprehensiveAndProvince")
    public Result<Page> findUserInfoByComprehensiveAndProvince(UserInfo userInfo,
                               @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                               @RequestParam(name="size",required = false,defaultValue = "10")Integer size) {
        //JPA有一个通用分页查询的接口 Pageable 是Spring下的Pageable
        if(page<1){
            throw new BizException("【查询所有用户】:当前页码不能小于1");
        }
        Sort.Order order=Sort.Order.desc("comprehensive");//按照综合素质降序查询
        Sort sort=Sort.by(order);
        //都是从0开始 所以page-1
        Pageable pageable= PageRequest.of(page-1,size,sort);
        Page pageResult = userInfoService.findUserInfo(userInfo,pageable);
        return ResultUtil.success(pageResult);
    }

    //综合榜中的全国榜
    @GetMapping("/findUserInfoByComprehensiveAndChina")
    public Result<Page> findUserInfoByComprehensiveAndChina(UserInfo userInfo,
                               @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                               @RequestParam(name="size",required = false,defaultValue = "10")Integer size) {
        //JPA有一个通用分页查询的接口 Pageable 是Spring下的Pageable
        if(page<1){
            throw new BizException("【查询所有用户】:当前页码不能小于1");
        }
        Sort.Order order=Sort.Order.desc("comprehensive");//按照综合素质降序查询
        Sort sort=Sort.by(order);
        //都是从0开始 所以page-1
        Pageable pageable= PageRequest.of(page-1,size,sort);
        Page pageResult = userInfoService.findUserInfo(userInfo,pageable);
        return ResultUtil.success(pageResult);
    }

    //智力榜中的省份榜
    @GetMapping("/findUserInfoByIntelligenceAndProvince")
    public Result<Page> findUserInfoByIntelligenceAndProvince(UserInfo userInfo,
                                                        @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                                                       @RequestParam(name="size",required = false,defaultValue = "10")Integer size) {
        //JPA有一个通用分页查询的接口 Pageable 是Spring下的Pageable
        if(page<1){
            throw new BizException("【查询所有用户】:当前页码不能小于1");
        }
        Sort.Order order=Sort.Order.desc("intelligence");//按照智力降序查询
        Sort sort=Sort.by(order);
        //都是从0开始 所以page-1
        Pageable pageable= PageRequest.of(page-1,size,sort);
        Page pageResult = userInfoService.findUserInfo(userInfo,pageable);
        return ResultUtil.success(pageResult);
    }

    //智力榜中的全国榜
    @GetMapping("/findUserInfoByIntelligenceAndChina")
    public Result<Page> findUserInfoByIntelligenceAndChina(UserInfo userInfo,
                               @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                               @RequestParam(name="size",required = false,defaultValue = "10")Integer size) {
        //JPA有一个通用分页查询的接口 Pageable 是Spring下的Pageable
        if(page<1){
            throw new BizException("【查询所有用户】:当前页码不能小于1");
        }
        Sort.Order order=Sort.Order.desc("intelligence");//按照智力降序查询
        Sort sort=Sort.by(order);
        //都是从0开始 所以page-1
        Pageable pageable= PageRequest.of(page-1,size,sort);
        Page pageResult = userInfoService.findUserInfo(userInfo,pageable);
        return ResultUtil.success(pageResult);
    }

    //体力榜中的省份榜
    @GetMapping("/findUserInfoByPowerAndProvince")
    public Result<Page> findUserInfoByPowerAndProvince(UserInfo userInfo,
                                                      @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                                                      @RequestParam(name="size",required = false,defaultValue = "10")Integer size) {
        //JPA有一个通用分页查询的接口 Pageable 是Spring下的Pageable
        if(page<1){
            throw new BizException("【查询所有用户】:当前页码不能小于1");
        }
        Sort.Order order=Sort.Order.desc("power");//按照体力降序查询
        Sort sort=Sort.by(order);
        //都是从0开始 所以page-1
        Pageable pageable= PageRequest.of(page-1,size,sort);
        Page pageResult = userInfoService.findUserInfo(userInfo,pageable);
        return ResultUtil.success(pageResult);
    }

    //体力榜中的全国榜
    @GetMapping("/findUserInfoByPowerAndChina")
    public Result<Page> findUserInfoByPowerAndChina(UserInfo userInfo,
                                                     @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                                                     @RequestParam(name="size",required = false,defaultValue = "10")Integer size) {
        //JPA有一个通用分页查询的接口 Pageable 是Spring下的Pageable
        if(page<1){
            throw new BizException("【查询所有用户】:当前页码不能小于1");
        }
        Sort.Order order=Sort.Order.desc("power");//按照体力降序查询
        Sort sort=Sort.by(order);
        //都是从0开始 所以page-1
        Pageable pageable= PageRequest.of(page-1,size,sort);
        Page pageResult = userInfoService.findUserInfo(userInfo,pageable);
        return ResultUtil.success(pageResult);
    }

    //魅力榜中的省份榜
    @GetMapping("/findUserInfoByCharmAndProvince")
    public Result<Page> findUserInfoByCharmAndProvince(UserInfo userInfo,
                                               @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                                               @RequestParam(name="size",required = false,defaultValue = "10")Integer size) {
        //JPA有一个通用分页查询的接口 Pageable 是Spring下的Pageable
        if(page<1){
            throw new BizException("【查询所有用户】:当前页码不能小于1");
        }
        Sort.Order order=Sort.Order.desc("charm");//按照体力降序查询
        Sort sort=Sort.by(order);
        //都是从0开始 所以page-1
        Pageable pageable= PageRequest.of(page-1,size,sort);
        Page pageResult = userInfoService.findUserInfo(userInfo,pageable);
        return ResultUtil.success(pageResult);
    }

    //魅力榜中的全国榜
    @GetMapping("/findUserInfoByCharmAndChina")
    public Result<Page> findUserInfoByCharmAndChina(UserInfo userInfo,
                                              @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                                              @RequestParam(name="size",required = false,defaultValue = "10")Integer size) {
        //JPA有一个通用分页查询的接口 Pageable 是Spring下的Pageable
        if(page<1){
            throw new BizException("【查询所有用户】:当前页码不能小于1");
        }
        Sort.Order order=Sort.Order.desc("charm");//按照体力降序查询
        Sort sort=Sort.by(order);
        //都是从0开始 所以page-1
        Pageable pageable= PageRequest.of(page-1,size,sort);
        Page pageResult = userInfoService.findUserInfo(userInfo,pageable);
        return ResultUtil.success(pageResult);
    }
}
